 layui.config({
 			base: './../../layui/lay/modules/'
 	});
 
layui.use(['form','layer','laydate','table','laytpl','notice'],function(){
	
var flow = layui.flow;
	
	
		
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		notice=layui.notice,
		table = layui.table;
			
			
	const typeVue=new Vue({
		el: '#booktype',
		data:{
			all_data:[]
		},
		methods:{
			getAllCategory:function(){
				this.$http.post(global_url+'/admin/novel/categorys/all')
					.then(
					response=>{
						
						this.all_data=response.body.datas.category;
						table.render({
							elem: '#booktype',
							cellMinWidth : 95,
							cols : [[
								{field:"id", title: 'ID', width: 60, fixed:"left",sort:"true", align:'center'},
								{field: 'categoryName', title: '类型名称', align:'center', edit: 'text'},
								{field: 'categorySonId', title: '父类类别ID', align:'center'},
								{title: '操作', minWidth:170, templet:'#typeListBar',fixed:"right",align:"center"}
							]],
							data:this.all_data,
																				
						});
						
						
						
					},					
					response=>{
						layer.msg(response.body.status)
					})
				
			},
			updataCategory:function(data){
				var index=layer.load();
				this.$http.post(global_url+'/admin/category/updata',data)
					.then(
					response=>{
						if(response.body.msg=="success")
							notice.init({
								type: "default",
								autoClose: true,
								title: "处理成功",
								desktop: true,
							});
							
						
						else{
							notice.init({
								type: "danger",
								autoClose: true,
								title: response.body.msg,
								desktop: true,
							});
						}
						layer.close(index)

						
					},					
					response=>{
						layer.msg(response.body.status)
						layer.close(index)

					})
			},
			deleteCategory:function(id){
				var index=layer.load();
				this.$http.post(global_url+'/admin/category/delete/'+id)
					.then(
					response=>{
						if(response.body.msg=="success")
							notice.init({
								type: "default",
								autoClose: true,
								title: "处理成功",
								desktop: true,
							});
							
						
						else{
							notice.init({
								type: "danger",
								autoClose: true,
								title: response.body.msg,
								desktop: true,
							});
						}
						layer.close(index)

						
					},					
					response=>{
						layer.msg(response.body.status)
						layer.close(index)

					})
			}
			
		}}); 
		
		
		typeVue.getAllCategory()
		
		
		
	  //监听行工具事件
	  table.on('tool(booktype)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据
		,layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'updata'){
			typeVue.updataCategory(data)
		} else if(layEvent === 'del'){
		  layer.confirm('真的删除行么', function(index){
			obj.del(); //删除对应行（tr）的DOM结构
			layer.close(index);
			//向服务端发送删除指令
			typeVue.deleteCategory(data.id)
		  });
		}
		 
	  });
		
	
	$(".addType").on("click",function(){
			var index = layui.layer.open({
				title : "添加类型",
				type : 2,
				content : "typeAdd.html",
				success : function(layero, index){
// 						var body = layui.layer.getChildFrame('body', index);
// 						if(permissions){
// 							roleVue.permissions=permissions;
// 							var newHtml=roleVue.addPermissions();
// 							// var newHtml ='<input type="checkbox" checked lay-skin="switch" lay-filter="encrypt" title="是否加密">';
// 							
// 							body.find(".roleName").val("ROLE_");  
// 							body.find(".pcheckbox").append(newHtml);
// 							form.render(null,"test1");
// 
// 						}
 				
						setTimeout(function(){
								layui.layer.tips('点击此处返回权限管理', '.layui-layer-setwin .layui-layer-close', {
										tips: 3
								});
						},500)
				}
			})
			layui.layer.full(index);
			window.sessionStorage.setItem("index",index);
			$(window).on("resize",function(){
					layui.layer.full(window.sessionStorage.getItem("index"));
			})
			
	});
		
})
